$(document).ready(function(){
	
	$(".panel").addClass('magictime puffIn');
	/*
	$(".panel").addClass('magictime swap');
	*/
	//var apiurl = "http://www.yike.com/index.php/home/index";
	var apiurl = "http://118.190.81.72/index.php/home/index";
	
	var jsonp = "callback";
	
	//登录
	$.ajax({
		type: "post",
		async: false,
		url: apiurl+"/login",
		dataType: "jsonp",
		data:{
			user:"user",
			passwd:"666",
		},
		jsonp: jsonp,
		success: function(json){
			//console.log(json);
			//console.log(json.code);
		},
		error: function(){
			console.log('异常错误');
		}
	});
	//控制
	$(".sys-control").click(function(){
		var command = parseInt($(this).attr("data-value"));
		if(window.dang == command) {
			console.log('目前已经是这个档位');
			return false;
		}
		$.ajax({
			type: "post",
			async: false,
			url: apiurl+"/control",
			dataType: "jsonp",
			data:{
				command:command,
			},
			jsonp: jsonp,
			success: function(json){
				if(json.code == 200) {
					info();
				} else {
					console.log(json.desc);
				}
			},
			error: function(){
				console.log('异常错误');
			}
		});
	})

	
	info();
	//获取信息
	function info(){
		$.ajax({
			type: "post",
			async: false,
			url: apiurl+"/collector",
			data:{
				action:'getinfo',  //部分程序   不允许提交空post数据
			},
			dataType: "jsonp",
			jsonp: jsonp,
			success: function(json){
				if(json.code == 200) {
					var data = json.data;
					var PM25 = 0;
					for(var i in data){  
						var Name = data[i].Name;
						var Value = data[i].Value;
						if(Name == "PM2_5") {
							Name = "sanjiao-left";
						}
						if(Name == "PM2.5") {
							Name = "sanjiao-right";
							PM25 = Value;
						}
						if(Name == "ST") {
							window.dang = parseInt(Value);  //设备的档数
						}
						$("#"+Name+" .value").text(Value);  //更新数据
						//高亮 1 2 3 4 0
						if(window.dang > 0) {
							$("#btn").addClass("on");  //高亮 0
							$("#number-box a").removeClass("on"); //取消1 2 3 4
							$("#number-"+window.dang).addClass("on");//当前值高亮 
						} else {	
							$("#btn").removeClass("on"); //取消 0
							$("#number-box a").removeClass("on"); //取消1 2 3 4
						}
					}  
					$("#PM25 .value").text(PM25);
					console.log("当前档数:" + window.dang);
				} else {
					console.log(json.desc);
				}
			},
			error: function(){
				console.log('异常错误');
			}
		});
	}
})